package com.demo.consumer.controller;

import com.alibaba.fastjson.JSON;
import com.demo.base.pojo.Clothetype;
import com.demo.base.pojo.Clothinginfo;
import com.demo.base.pojo.Styletype;
import com.demo.base.pojo.pagedDate;
import com.demo.base.service.ClothinginfoService;
import com.demo.base.util.SystemCode;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
@Controller
public class HomeController {
    @Reference
    ClothinginfoService clothinginfoService;

    @RequestMapping("show")
    public String show(@RequestParam(value = "gender", defaultValue = "-1") int gender, @RequestParam(value = "age", defaultValue = "-1") int age, @RequestParam(value = "color", required = false) String color, @RequestParam(value = "styleid", defaultValue = "-1") int styleid, @RequestParam(value = "typeid", defaultValue = "-1") int typeid, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, HttpServletRequest request) {
        int pageSize = 3;
        pagedDate<Clothinginfo> pagedDate = clothinginfoService.pagedDate(gender,age,color,styleid,typeid,pageIndex,pageSize);
        request.setAttribute("pagedDate", pagedDate);
        List<Styletype> all = clothinginfoService.getall();
        request.setAttribute("all", all);
        List<Clothetype> type = clothinginfoService.gettype();
        request.setAttribute("type", type);
        return "show";
    }

    @RequestMapping("search")
    public String search(HttpServletRequest request) {
        List<Styletype> all = clothinginfoService.getall();
        request.setAttribute("all", all);
        List<Clothetype> type = clothinginfoService.gettype();
        request.setAttribute("type", type);
        return "search";
    }

    @RequestMapping(value = "info", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String info(int id) throws IOException {
        File file = new File(id+"号搭配信息.txt");
        file.createNewFile();
        Clothinginfo info = clothinginfoService.info(id);
        List<Styletype> getall = clothinginfoService.getall();
        List<Clothetype> gettype = clothinginfoService.gettype();
        for (Styletype style : getall) {
            if (style.getId() == info.getStyleid()) {
                info.setStylename(style.getApply());
            }
        }
        for (Clothetype type : gettype) {
            if (type.getId() == info.getTypeid()) {
                info.setTypename(type.getTypename());
            }
        }
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        String fileName = bufferedReader.readLine();
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("编号:" + info.getId() + "\t" + "类型:" + info.getTypename() + "\t" + "型号:"  + info.getModel() + "\t" + "颜色:" + info.getColor() + "\t" + "风格:" + info.getStylename() + "\t" + "适用人群:" + info.getIntended() + "\t" + "年龄:" + info.getAge());
        fileWriter.flush();
        fileWriter.close();
        if(fileName!=null){
            return JSON.toJSONString(SystemCode.ERROR_CODE);
        }else{
            return JSON.toJSONString(SystemCode.SUCCESS_CODE);
        }
    }
}

